#define  _CRT_SECURE_NO_WARNINGS
#include <vector>
using namespace std;


/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> rightSideView(TreeNode* root) {
        queue<TreeNode*> q;
        q.push(root);
        vector<int> ret;
        if (!root) return ret;
        while (!q.empty())
        {
            int n = q.size();
            while (n--)
            {
                TreeNode* node = q.front();
                q.pop();
                if (n == 0) ret.push_back(node->val);
                if (node->left) q.push(node->left);
                if (node->right) q.push(node->right);  //
            }
        }
        return ret;
    }
};